﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SharePointClientHelper
{
    public partial class MainParent : Form
    {
        public MainParent()
        {
            InitializeComponent();
            Login loginForm = new Login();
            loginForm.BringToFront();
            DialogResult diagResult = loginForm.ShowDialog();

            if (diagResult == System.Windows.Forms.DialogResult.OK)
            {
                SPClientHelper spcHelper = SPClientHelper.GetSPClientHelper();
                this.Text = spcHelper.SiteTitle;
                loginToolStripMenuItem.Text = "Logout";

                this.toolStripStatusLabel.Text = spcHelper.GetSiteUrl();
                this.toolStripLblWelcome.Text = "Welcome " + spcHelper.GetCurrentUser();
            }
            else
            {
                EnableDisable(false);
            }
        }

        private void ShowNewForm(object sender, EventArgs e)
        {
            AddUsers childForm = new AddUsers();
            ShowForm(childForm, "Add Users");
        }

        private void OpenFile(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            openFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
            if (openFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                string FileName = openFileDialog.FileName;
            }
        }

        private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            saveFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
            if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                string FileName = saveFileDialog.FileName;
            }
        }

        private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void CutToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void CopyToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void PasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        //private void ToolBarToolStripMenuItem_Click(object sender, EventArgs e)
        //{
        //    toolStrip.Visible = toolBarToolStripMenuItem.Checked;
        //}

        //private void StatusBarToolStripMenuItem_Click(object sender, EventArgs e)
        //{
        //    statusStrip.Visible = statusBarToolStripMenuItem.Checked;
        //}

        private void CascadeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void TileVerticalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }

        private void TileHorizontalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void ArrangeIconsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.ArrangeIcons);
        }

        private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form childForm in MdiChildren)
            {
                childForm.Close();
            }
        }

        private void ShowAddUsersForm(object sender, EventArgs e)
        {
            AddUsers childForm = new AddUsers();
            ShowForm(childForm, "Add Users");
        }

        private void ShowForm(Form childForm,string Title)
        {
            childForm.MdiParent = this;
            childForm.Text = Title;
            childForm.Show();
        }

        private void loginToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (loginToolStripMenuItem.Text == "Login")
            {
                Login loginForm = new Login();
                loginForm.BringToFront();
                DialogResult diagResult = loginForm.ShowDialog();

                if (diagResult == System.Windows.Forms.DialogResult.OK)
                {
                    this.Text = SPClientHelper.GetSPClientHelper().SiteTitle;
                    loginToolStripMenuItem.Text = "Logout";
                    EnableDisable(true);
                }
            }
            else
            {
                bool isLoggedOut = SPClientHelper.GetSPClientHelper().Logout();
                if (isLoggedOut)
                {
                    this.Text = "";
                    this.toolStripLblWelcome.Text = "";
                    this.toolStripStatusLabel.Text = "";
                    EnableDisable(false);
                    loginToolStripMenuItem.Text = "Login";
                }
            }
        }

        private void EnableDisable(bool enabled)
        {
            newToolStripMenuItem.Enabled = false;
            newToolStripButton.Enabled = false; 
        }

        private void MoveFile(object sender, EventArgs e)
        {
            //MoveDoc childForm = new MoveDoc();
            //ShowForm(childForm, "Move Documents");
        }

        private void GetUsers(object sender, EventArgs e)
        {
            GetUsersInGroup childForm = new GetUsersInGroup();
            ShowForm(childForm, "Get Users");
        }

        private void roleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AssignPermission childForm = new AssignPermission();
            ShowForm(childForm, "Assign Permissions");
        }
    }
}
